package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.CommodityMapper;
import com.tc.pojo.Commodity;
import com.tc.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:Commodity业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;


    /**
     * Commodity条件+分页查询
     * @param commodity 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Commodity> findPage(Commodity commodity, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(commodity);
        //执行搜索
        return new PageInfo<Commodity>(commodityMapper.selectByExample(example));
    }

    /**
     * Commodity分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Commodity> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Commodity>(commodityMapper.selectAll());
    }

    /**
     * Commodity条件查询
     * @param commodity
     * @return
     */
    @Override
    public List<Commodity> findList(Commodity commodity){
        //构建查询条件
        Example example = createExample(commodity);
        //根据构建的条件查询数据
        return commodityMapper.selectByExample(example);
    }


    /**
     * Commodity构建查询对象
     * @param commodity
     * @return
     */
    public Example createExample(Commodity commodity){
        Example example=new Example(Commodity.class);
        Example.Criteria criteria = example.createCriteria();
        if(commodity!=null){
            // 编号
            if(!StringUtils.isEmpty(commodity.getId())){
                    criteria.andEqualTo("id",commodity.getId());
            }
            // 关联的商家ID
            if(!StringUtils.isEmpty(commodity.getPid())){
                    criteria.andEqualTo("pid",commodity.getPid());
            }
            // 商品名称
            if(!StringUtils.isEmpty(commodity.getCdyname())){
                    criteria.andLike("cdyname","%"+commodity.getCdyname()+"%");
            }
            // 商品的小图片
            if(!StringUtils.isEmpty(commodity.getCdyimg())){
                    criteria.andEqualTo("cdyimg",commodity.getCdyimg());
            }
            // 头信息
            if(!StringUtils.isEmpty(commodity.getCdytitle())){
                    criteria.andLike("cdytitle","%"+commodity.getCdytitle()+"%");
            }
            // 是否启用 0启用 1禁用
            if(!StringUtils.isEmpty(commodity.getIsdisable())){
                    criteria.andEqualTo("isdisable",commodity.getIsdisable());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        commodityMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Commodity
     * @param commodity
     */
    @Override
    public void update(Commodity commodity){
        commodityMapper.updateByPrimaryKey(commodity);
    }

    /**
     * 增加Commodity
     * @param commodity
     */
    @Override
    public void add(Commodity commodity){
        commodityMapper.insert(commodity);
    }

    /**
     * 根据ID查询Commodity
     * @param id
     * @return
     */
    @Override
    public Commodity findById(Integer id){
        return  commodityMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Commodity全部数据
     * @return
     */
    @Override
    public List<Commodity> findAll() {
        return commodityMapper.selectAll();
    }
}
